from django.db import models
from charts.models import Host
from users.models import Ops_user


# Create your models here.
class Monitorpy(models.Model):
    py_status_choices = (
        (0, '启动'),
        (1, '关闭'),
    )
    py_title = models.CharField(max_length=32, verbose_name='脚本名称')
    file_name = models.CharField(max_length=32, verbose_name='脚本文件名称')
    py_describe = models.CharField(max_length=32, null=True, verbose_name='脚本描述')
    py_timer = models.CharField(max_length=32, null=True, verbose_name='定时器')
    py_status = models.SmallIntegerField(choices=py_status_choices, default=0, verbose_name='脚本状态')
    start_time = models.DateTimeField(auto_now=True, verbose_name='启动时间')
    host_ip = models.CharField(max_length=16, null=True, verbose_name='主机表ID')

    def py_status_name(self):
        return self.get_py_status_display()

    class Meta:
        db_table = 'ops_monitorpy'
        verbose_name = '监控脚本表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.host_ip + '|' + self.file_name


class Fork_version(models.Model):
    ver_name = models.CharField(max_length=128, verbose_name='版本名称')
    ver_time = models.DateTimeField(auto_now_add=True, verbose_name='发布版本时间')
    user = models.ForeignKey(to='users.Ops_user', on_delete=models.DO_NOTHING, null=True, blank=True,
                             verbose_name='用户表ID', db_constraint=False)
    field1 = models.CharField(max_length=16, null=True, verbose_name='预留字段1')
    field2 = models.CharField(max_length=16, null=True, verbose_name='预留字段2')

    def ver_name_time(self):
        return str(self.ver_time) + self.ver_name

    class Meta:
        db_table = 'ops_fork_version'
        verbose_name = '监控脚本表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return str(self.ver_time) + self.ver_name


class Log_audit(models.Model):
    '''
    日志审计表
    '''
    tag_name = models.CharField(max_length=16, verbose_name='栏目名称')
    log_info = models.CharField(max_length=256, verbose_name='操作信息')
    user = models.ForeignKey(to='users.Ops_user', on_delete=models.DO_NOTHING, null=True, blank=True,
                             verbose_name='用户表ID', db_constraint=False)
    handle_time = models.DateTimeField(auto_now_add=True, verbose_name='操作时间')
    field1 = models.CharField(max_length=16, null=True, verbose_name='预留字段1')
    field2 = models.CharField(max_length=16, null=True, verbose_name='预留字段2')

    def user_name(self):
        return self.user.username

    class Meta:
        db_table = 'ops_log_audit'
        verbose_name = '监控脚本表'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.tag_name
